package com.jell.purchase.web;
import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.Page;
import com.jell.purchase.Constants;
import com.jell.purchase.ResultFactory;
import com.jell.purchase.exception.ServiceException;
import com.jell.purchase.model.GoodsInfoDo;
import com.jell.purchase.model.UserDo;
import com.jell.purchase.model.po.GoodsCreatePO;
import com.jell.purchase.model.po.GoodsListPO;
import com.jell.purchase.model.vo.GoodsListVO;
import com.jell.purchase.service.GoodsInfoService;
import com.jell.purchase.vo.PageVO;
import com.jell.purchase.vo.Result;
import com.jell.purchase.vo.ResultData;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by linx on 2018-04-17.
 */
@RestController
@RequestMapping({"/api/v1/p/manage/goods/","/api/v1/m/manage/goods/"})
public class GoodsInfoController {
    private static final Logger logger = Logger.getLogger(GoodsInfoController.class);

    @Autowired
    GoodsInfoService goodsInfoService;

    @RequestMapping(value={"/detail"}, method=RequestMethod.GET)
    public ResultData selectGoodsInfoById(Integer id){
        try {
            return ResultFactory.createSuccessResultData(goodsInfoService.getGoodsInfoById(id));
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("查询商品详情异常:",e);
            return ResultFactory.createSuccessResultData("查询商品详情异常");

        }
    }


    @RequestMapping("/list")
    @ResponseBody
    public ResultData<?> list (@Valid GoodsListPO po, BindingResult result){
        List<String> errorList = new ArrayList<String>();
        try {
            if (result.hasErrors()) {
                List<FieldError> fieldErrors = result.getFieldErrors();
                for (FieldError fieldError : fieldErrors) {
                    errorList.add(fieldError.getDefaultMessage());
                }
            }
            if(errorList.size() > 0){
                return ResultFactory.createFailResultData(errorList.get(0), null);
            }
            return ResultFactory.createSuccessResultData( goodsInfoService.selectGoodsList(po));
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("列表查询商品异常:",e);
            return ResultFactory.createSuccessResultData("列表查询商品异常");

        }

    }
    @RequestMapping("/queryByChName")
    @ResponseBody
    public ResultData<?> queryByChName (@Valid GoodsListPO po, BindingResult result){
        List<String> errorList = new ArrayList<String>();
        try {
            if (result.hasErrors()) {
                List<FieldError> fieldErrors = result.getFieldErrors();
                for (FieldError fieldError : fieldErrors) {
                    errorList.add(fieldError.getDefaultMessage());
                }
            }
            if(errorList.size() > 0){
                return ResultFactory.createFailResultData(errorList.get(0), null);
            }
            return ResultFactory.createSuccessResultData( goodsInfoService.selectGoodsListByName(po));
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("列表查询商品异常:",e);
            return ResultFactory.createSuccessResultData("列表查询商品异常");

        }

    }
    @RequestMapping("/queryById")
    @ResponseBody
    public ResultData<?> queryById (Integer goodsId){
        try {
            return ResultFactory.createSuccessResultData( goodsInfoService.selectGoodsById(goodsId));
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("查询商品异常:",e);
            return ResultFactory.createSuccessResultData("查询商品异常");

        }

    }


    @RequestMapping("/create")
    @ResponseBody
    public Result create(@Valid @RequestBody GoodsCreatePO po, BindingResult result){
        StringBuffer sb = new StringBuffer();
        try {
            if (result.hasErrors()) {
                List<FieldError> fieldErrors = result.getFieldErrors();
                for (FieldError fieldError : fieldErrors) {
                    sb.append(fieldError.getDefaultMessage());
                }
            }
            if(sb.length() > 0){
                return new Result(Constants.RESULT_CODE_FAIL,sb.toString());
            }
            goodsInfoService.create(po);
            return ResultFactory.createSuccessResultData(null);
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("商品新增异常:",e);
            return ResultFactory.createSuccessResultData("商品新增异常");

        }
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer id, BindingResult result){
        try {
            goodsInfoService.deleteById(id);
            return ResultFactory.createSuccessResultData(null);
        }catch (ServiceException e){
            return ResultFactory.createFailResultData(e.getMessage());
        }catch (Exception e){
            logger.error("删除商品详情异常:",e);
            return ResultFactory.createSuccessResultData("删除商品详情异常");

        }
    }

}
